// pages/canteen/addAddress/addAddress.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    session_key: '',//token
    userName: '',//收货人
    phoneNum: '',//手机号
    province: '',//省
    city:'',//市
    zone:'',//区
    xiangxi: '',//详细地址
    is_default: '',//是否默认
    page_title: '',//信息标题
    fromPage:'',
    
    //地址类型
    address_typelist:[
      { id: "1", name: "公司" },
      { id: "2", name: "住宅" },
      { id: "3", name: "学校" },
      { id: "4", name: "其他" },
    ],
    leixingid:"",//类型id
    leixingindex:"",//类型索引

    prevPage: '',//上一级页面类型
    type: '',//页面类型
    addressInfo: '',//获取编辑的时候的信息
    _id: '',//需要修改的id
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.info(options);
    var _that = this;
    _that.setData({
      session_key: wx.getStorageSync("token"),
      type: options.type
    })
    if (options.addreddId != undefined) {
      _that.setData({
        _id: options.addreddId
      })
    }
    if (options.pagesType != undefined) {
      _that.setData({
        prevPage: options.pagesType
      })
    }

    if (options.fromPage == 'confirmOrder') {
      _that.setData({
        fromPage:'confirmOrder'
      })
    }
    _that.setData({
      session_key: wx.getStorageSync("token"),
      type: options.type,
      _id: options.addreddId
    })
    if (options.type == "new") {
      _that.setData({
        page_title: '请填写收货地址'
      })
    } else {
      wx.showLoading({
        title: '数据加载中',
      })
      _that.setData({
        page_title: '编辑收货地址'
      })
      var _addressId = options.addreddId;
      wx.request({
        url: app.globalData.httpsPath + '/user/address/info',
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        data: {
          token: wx.getStorageSync("token"),
          id: _addressId,
        },
        //请求后台数据成功  
        success: function (res) {
          //console.info(res);
          if (res.data.status == true) {
            var addressInfo = res.data.data;
            var  _province, _city, _zone;
            if (addressInfo.province == null) {
              _province = "";
            } else {
              _province = addressInfo.province
            }
            if (addressInfo.city == null) {
              _city = "";
            } else {
              _city = addressInfo.city
            }
            if (addressInfo.zone == null) {
              _zone = "";
            } else {
              _zone = addressInfo.zone
            }
           
            _that.setData({
              userName: addressInfo.truename,//收货人
              phoneNum: addressInfo.mobile,//手机号
              province:_province,//省
              city: _city,//市
              zone: _zone,//区
              xiangxi: addressInfo.address,//详细地址
              is_default: addressInfo.is_default,//是否默认
            })
            // 隐藏加载框
            wx.hideLoading();
          }
        }
      })
    }
  },
  //姓名
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  //手机号
  getPhone: function (e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },
  // 详细地址
  detailedAddress: function (e) {
    this.setData({
      xiangxi: e.detail.value
    })
  },
  //获取定位
  getLocation: function () {
    var that = this;
   

   
 
    wx.chooseAddress({
      success(res) {

        that.setData({
          userName: res.userName,//收货人
          phoneNum: res.telNumber,//手机号
          province: res.provinceName,//省
          city: res.cityName,//市
          zone: res.countyName,//区
          xiangxi: res.detailInfo,//详细地址
          is_default: 0,//是否默认
        })
       
      }
    })
          },
       
    

  //切换类型
  switchTypeTab: function (e) {
    // 获取item项的id，和数组的下标值  
    let id = e.currentTarget.dataset.id,
      index = parseInt(e.currentTarget.dataset.index);
    // 把点击到的某一项，设为当前index  
    this.setData({
      leixingid: id,
      leixingindex: index
    })
  },

  //添加新地址
  subAddress: function () {
    var that = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    var _token = that.data.session_key;
    var _prevPage = that.data.prevPage;
    if (that.data.userName == '') {
      wx.showToast({
        icon: "none",
        title: '请输入收货人'
      })
      return false;
    }
    if (that.data.phoneNum == '') {
      wx.showToast({
        icon: "none",
        title: '请输入手机号码',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(that.data.phoneNum)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (that.data.province == '') {
      wx.showToast({
        icon: "none",
        title: '请选择省市区'
      })
      return false;
    }
    if (that.data.xiangxi == '') {
      wx.showToast({
        icon: "none",
        title: '请输入详细地址'
      })
      return false;
    }
    if (that.data.is_default == "") {
      that.setData({
        is_default: '0'
      })
    }

    //添加地址
    wx.request({
      url: app.globalData.httpsPath + '/user/address/add', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        token: _token,
        truename: that.data.userName,
        mobile: that.data.phoneNum,
        province: that.data.province,
        city: that.data.city,
        zone: that.data.zone,
        address: that.data.xiangxi,
        is_default: that.data.is_default,
      },
      success(res) {
        console.info(res)
        if (res.data.status == true) {
          wx.showToast({
            title: '添加完成',
            icon: 'succes',
            duration: 3000,
            mask: true
          })
          // 如果是从提交订单页面来的直接调回去
          if(that.data.fromPage == 'confirmOrder') {
            wx.setStorageSync('addressInfo', res.data.data)
            wx.navigateTo({
              url: '../confirmOrder/confirmOrder?pagesType=selected',
            })
            return false
          }
          
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/addressList/addressList?pagesType=' + _prevPage,
            })
          }, 3000)
        }
      }
    })
  },
  //修改地址
  subAddressEdit: function (e) {
    var that = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    var _token = that.data.session_key;
    var _id = e.currentTarget.dataset.id;
    var _prevPage = that.data.prevPage;
    console.info(_prevPage);


    if (that.data.userName == '') {
      wx.showToast({
        icon: "none",
        title: '请输入收货人'
      })
      return false;
    }
    if (that.data.phoneNum == '') {
      wx.showToast({
        icon: "none",
        title: '请输入手机号码',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(that.data.phoneNum)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (that.data.province == '') {
      wx.showToast({
        icon: "none",
        title: '请选择省市区'
      })
      return false;
    }
    if (that.data.xiangxi == '') {
      wx.showToast({
        icon: "none",
        title: '请输入详细地址'
      })
      return false;
    }
    if (that.data.is_default == "") {
      that.setData({
        is_default: '0'
      })
    }

    //修改地址
    wx.request({
      url: app.globalData.httpsPath + '/user/address/update', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        token: _token,
        truename: that.data.userName,
        mobile: that.data.phoneNum,
        province: that.data.province,
        city: that.data.city,
        zone: that.data.zone,
        address: that.data.xiangxi,
        is_default: that.data.is_default,
        id: _id,
      },
      success(res) {
        console.info(res)
        if (res.data.status == true) {
          wx.showToast({
            title: '修改成功',
            icon: 'succes',
            duration: 3000,
            mask: true
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '../addressList/addressList?pagesType=' + _prevPage,
            })
          }, 3000)
        }
      }
    })
  },

})